package com.stylefeng.guns.core.util.clm;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiasheng.api.utils.SsoUtil;
import com.stylefeng.guns.modular.clm.controller.ClmRoomController;

/** 
 * @author  yc 
 * @date 创建时间：2018年5月8日 上午9:53:10  
 **/
public class ClmAccessUser {

	private final static Logger logger = LoggerFactory.getLogger(ClmRoomController.class);
	/**
	 * 获取第三方单点信息
	 * @return
	 */
	private static String getPassportClientJsonObj(){
		String userJsonObj =null;
		try {
			
			userJsonObj =SsoUtil.getUser(getRequest());
			logger.info("sso message userJsonObj=----"+userJsonObj);
		} catch (Exception e) {
			logger.info(e.getMessage());
			e.printStackTrace();
		}
		return userJsonObj;
	}

	/**
	 *根据用户类型获取第三方单点用户信息
	 */
	public static String getSSOUserId(){
		
		/**********验证用户是否在单点登录 Start ************/
		String userJsonObj = getPassportClientJsonObj();
		if(StringUtils.isBlank(userJsonObj)) return null;
		JSONObject jsonObj = JSON.parseObject(userJsonObj);
		String userId = jsonObj.getString("userid");
		return userId;
		
	}
	
	public static HttpServletRequest getRequest(){
		RequestAttributes  att = RequestContextHolder.getRequestAttributes();
		if(null!=att){
			return ((ServletRequestAttributes)att).getRequest();
		}
		return null;
	}
}
